/**
 * Copyright (c) 2012, QinJian
 * All rights reserved.
 */
package com.q.course.web.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.q.course.entity.category.Category;
import com.q.course.entity.classes.Classes;
import com.q.course.entity.user.User;
import com.q.course.service.category.CategoryService;
import com.q.course.service.classes.ClassesService;
import com.q.course.service.user.ShiroDbRealm.ShiroUser;
import com.q.course.service.user.UserService;


/**
 * @author 覃健,Email:j411@qq.com
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private ClassesService classesService;
	
	@RequestMapping( value="/login.do" , method = RequestMethod.GET )
	public String gotoLogin(){
		return "user/login";
	}
	
	@RequestMapping( value="/login.do" , method = RequestMethod.POST)
	public String fail(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String userName, Model model) {

		model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM, userName);
		return "user/login";
	}
	
	@RequestMapping( value="/loginsuccess.do" , method = RequestMethod.GET )
	public String gotoArrangeCourse(HttpSession session){
		
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		session.setAttribute("user", user);
		//地点
		List<Category> plist = categoryService.getCategories(6);
		session.setAttribute("plist", plist);
		//快捷时间
		List<Category> tlist = categoryService.getCategories(2);
		session.setAttribute("tlist", tlist);
		//班级
		List<Classes> cList = classesService.getClassList();
		session.setAttribute("clist", cList);
		return "arrange/arrange_course";
	}
	
	@ResponseBody
	@RequestMapping( value="/save.do" , method = RequestMethod.POST )
	public Object[] createUser( @ModelAttribute User user ){
		return userService.saveUser(user);
	}
	
	@ResponseBody
	@RequestMapping( value="/getarrter.do" , method = RequestMethod.GET )
	public List<Object[]> getArrangeTeachers( @RequestParam(value = "date") Date date ){
		return userService.getTeachers(date);
	}
	
	@ResponseBody
	@RequestMapping( value="/getterlist.do" , method = RequestMethod.GET )
	public List<User> getTeacherList(){
		return userService.getTeacherList();
	}
	
	@ResponseBody
	@RequestMapping( value="/getuserbyid.do" , method = RequestMethod.GET )
	public User getUserById( @RequestParam(value = "id") Long id ){
		return userService.getUserById(id);
	}
}
